package lg.minecraft.plugin.library.command;

import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;

/**
 * @author langle__
 * @version 1.0
 */
@Getter
public class CommandContext {

    private final CommandSender sender;
    private final String[] args;

    public CommandContext(CommandSender sender, String[] args) {
        this.sender = sender;
        this.args = args;
    }

    public boolean isPlayerSender() {
        return sender instanceof Player;
    }

    public boolean isConsoleSender() {
        return sender instanceof ConsoleCommandSender;
    }

    public Player getPlayer() {
        return sender instanceof Player ? (Player) sender : null;
    }

    public String getArg(int index) {
        return index >= args.length ? null : args[index];
    }

    public Player getArgPlayer(int index) {
        return Bukkit.getPlayer(this.getArg(index));
    }

    public Integer getArgInteger(int index) {
        return Integer.parseInt(this.getArg(index));
    }

    public Boolean getArgBoolean(int index) {
        return Boolean.parseBoolean(this.getArg(index));
    }

    public int getArgLength() {
        return args.length;
    }

}
